﻿Imports System.Data.SqlClient
Public Class ThemLop

    Dim Khoa_ID As Object
    Dim Lop_ID As Object
    Private _lop_ID As Object

    Sub New(ByVal Lop_ID As Object)
        ' TODO: Complete member initialization 
        _lop_ID = Lop_ID
    End Sub

    Private Sub ThemLop_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim con As SqlConnection = New SqlConnection()
        con.ConnectionString = Ketnoi.str
        con.Open()
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.Connection = con
        cmd.CommandText = "SELECT * FROM Khoa"
        Dim rd As SqlDataReader
        rd = cmd.ExecuteReader()
        Dim td As DataTable = New DataTable()
        td.Load(rd)
        Dim i As Integer
        For i = 0 To td.Rows.Count - 1 Step i + 1
            Me.cboKhoa.Items.Add(td.Rows(i)(1))
        Next
    End Sub

    Private Sub btnThemLop_Click(sender As System.Object, e As System.EventArgs) Handles btnThemLop.Click
        Dim con As SqlConnection = New SqlConnection()
        con.ConnectionString = Ketnoi.str
        con.Open()
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.Connection = con
        Dim TenKhoa As String
        TenKhoa = cboKhoa.SelectedItem.ToString()
        cmd.CommandText = "SELECT Khoa_ID FROM Khoa WHERE TenKhoa='" + TenKhoa + "'"
        Dim rd As SqlDataReader
        rd = cmd.ExecuteReader()
        Dim td As DataTable = New DataTable()
        td.Load(rd)
        Dim MaKhoa As String
        MaKhoa = td.Rows(0)(0).ToString()

        cmd.CommandText = "INSERT INTO Lop VALUES('" + txtMaLop.Text + "','" + txtTenLop.Text + "','" + MaKhoa + "')"
        cmd.ExecuteNonQuery()
        Dim result As DialogResult
        result = MessageBox.Show("THÊM DỮ LIỆU THÀNH CÔNG", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        If result = DialogResult.OK Then
            Me.Close()
            Dim frm As New ThemLop(Lop_ID)
            frm.Show()
        End If
    End Sub

    Private Sub btnThoat_Click(sender As System.Object, e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
        Dim frm As New DanhSachLop(Lop_ID)
        frm.Show()
    End Sub
End Class